package io.gitee.zhangbinhub.admin.oauth.controller.inner

import cn.dev33.satoken.stp.StpUtil
import io.gitee.zhangbinhub.acp.boot.exceptions.WebException
import io.gitee.zhangbinhub.acp.boot.log.LogAdapter
import io.gitee.zhangbinhub.admin.common.api.CommonPath
import io.gitee.zhangbinhub.admin.common.api.OauthApi
import io.gitee.zhangbinhub.admin.oauth.entity.Organization
import io.gitee.zhangbinhub.admin.oauth.mapper.OrganizationMapper
import io.gitee.zhangbinhub.admin.oauth.service.OrganizationService
import io.gitee.zhangbinhub.admin.resource.server.base.BaseResourceServerController
import io.gitee.zhangbinhub.admin.resource.server.vo.OrganizationVo
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import org.noear.solon.annotation.Controller
import org.noear.solon.annotation.Inject
import org.noear.solon.annotation.Mapping
import org.noear.solon.core.handle.MethodType
import org.noear.solon.core.util.MimeType
import org.noear.solon.validation.annotation.Valid

@Valid
@Controller
@Api("机构信息（内部接口）")
open class InnerOrgController(
    @Inject logAdapter: LogAdapter,
    private val organizationService: OrganizationService
) : BaseResourceServerController(logAdapter) {
    private fun listToVo(organizationList: List<Organization>): List<OrganizationVo> =
        organizationList.map { organization -> OrganizationMapper.INSTANCE.entityToVo(organization) }

    @ApiOperation("获取所属机构及其所有子机构列表（所属机构）")
    @Mapping(
        method = [MethodType.GET],
        value = CommonPath.innerBasePath + OauthApi.currAndAllChildrenOrg,
        produces = MimeType.APPLICATION_JSON_VALUE
    )
    @Throws(WebException::class)
    fun currAndAllChildrenForOrg(): List<OrganizationVo> =
        listToVo(organizationService.getCurrAndAllChildrenForOrg(StpUtil.getLoginIdAsString()))

    @ApiOperation("获取所属机构及其所有子机构列表（管理机构）")
    @Mapping(
        method = [MethodType.GET],
        value = CommonPath.innerBasePath + OauthApi.currAndAllChildrenMngOrg,
        produces = MimeType.APPLICATION_JSON_VALUE
    )
    @Throws(WebException::class)
    fun currAndAllChildrenForMngOrg(): List<OrganizationVo> =
        listToVo(organizationService.getCurrAndAllChildrenForMngOrg(StpUtil.getLoginIdAsString()))

    @ApiOperation("获取所属机构及其所有子机构列表（所有机构）")
    @Mapping(
        method = [MethodType.GET],
        value = CommonPath.innerBasePath + OauthApi.currAndAllChildrenAllOrg,
        produces = MimeType.APPLICATION_JSON_VALUE
    )
    @Throws(WebException::class)
    fun currAndAllChildrenForAllOrg(): List<OrganizationVo> =
        listToVo(organizationService.getCurrAndAllChildrenForAllOrg(StpUtil.getLoginIdAsString()))
}